﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace SportingGoodsExhibitionSystem
{
    /// <summary>
    /// AdminAgree.xaml 的交互逻辑
    /// </summary>
    public partial class AdminAgree : Page
    {
        public AdminAgree()
        {
            InitializeComponent();
        }
        string strCon = Properties.Resources.strCON;

        public DataTable GetAllData()
        {
            
            SqlConnection con = new SqlConnection(strCon);
            
            SqlCommand cmd = new SqlCommand("select stunum as '学号', name as '姓名' from applyBack", con);
            //获取查询结果
            //SqlDataReader
            SqlDataAdapter read = new SqlDataAdapter(cmd);
            //保存查询结果
            DataTable dt = new DataTable();
            read.Fill(dt);
            return dt;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = GetAllData();
            dataGrid.ItemsSource = dt.DefaultView;
        }
        

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int num=0  ;
            try
            {
                DataRowView row = (DataRowView)dataGrid.SelectedItem;
                num = Convert.ToInt32(row["学号"].ToString());
                detailInfo detail = new detailInfo(num);
                detail.Show();
            }catch
            {
                MessageBox.Show("还未选择学生");
            }

        }

        public Boolean updateInfo(int num)
        {

            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentInfo set applyStatus=@sta where studentNumber=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = num;
            cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 1;
            //ExecuteNonQuery指令执行,更新操作
            cmd.ExecuteNonQuery();
            con.Close();
            return true;
        }

        public Boolean deleteInfo(int num)
        {

            SqlConnection con = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand("delete applyBack where stunum=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = num;
            con.Open();
            //ExecuteNonQuery指令执行,更新操作
            int i = cmd.ExecuteNonQuery();
            con.Close();
            if (i > 0) return true;
            return false;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid.SelectedItem;
            int num = Convert.ToInt32(row["学号"].ToString());
            updateInfo(num);
            deleteInfo(num);
            MessageBox.Show("确认通过申请成功");
            DataTable dt = GetAllData();
            dataGrid.ItemsSource = dt.DefaultView;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid.SelectedItem;
            int num = Convert.ToInt32(row["学号"].ToString());
            SqlConnection con = new SqlConnection(strCon);
            con.Open();
            SqlCommand cmd = new SqlCommand("update studentInfo set applyStatus=@sta where studentNumber=@id", con);
            cmd.Parameters.Add("@id", SqlDbType.Int, 10).Value = num;
            cmd.Parameters.Add("@sta", SqlDbType.Int, 10).Value = 0;
            //ExecuteNonQuery指令执行,更新操作
            cmd.ExecuteNonQuery();
            con.Close();
            deleteInfo(num);
            MessageBox.Show("已暂缓其返校");
            DataTable dt = GetAllData();
            dataGrid.ItemsSource = dt.DefaultView;
        }
    }
}
